package com.ruoyi.web.controller.material;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.material.MaterialType;
import com.ruoyi.system.service.IMaterialTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/3/6 10:56
 * 物料类型
 */
@Api(tags = "物料类型")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/material/type")
public class MaterialTypeController  extends BaseController {
    @Autowired
    IMaterialTypeService iMaterialTypeService;

    /**
     * 查询物料类型列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询物料类型")
    public TableDataInfo list(MaterialType baseTeam)
    {
        startPage();
        LambdaQueryWrapper<MaterialType> lqw = new LambdaQueryWrapper<MaterialType>();
        if (StringUtils.isNotBlank(baseTeam.getMaterialTypeCode())){
            lqw.eq(MaterialType::getMaterialTypeCode ,baseTeam.getMaterialTypeCode());
        }
        if (StringUtils.isNotBlank(baseTeam.getMaterialTypeName())){
            lqw.eq(MaterialType::getMaterialTypeName ,baseTeam.getMaterialTypeName());
        }
        List<MaterialType> list = iMaterialTypeService.list(lqw);
        return getDataTable(list);
    }


    /**
     * 新增物料类型
     */
    @Log(title = "物料单位" , businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增物料类型")
    @PostMapping
    public AjaxResult add(@RequestBody MaterialType baseTeam) {
        return toAjax(iMaterialTypeService.save(baseTeam) ? 1 : 0);
    }

    /**
     * 修改物料类型
     */
    @Log(title = "物料单位" , businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改物料类型")
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialType baseTeam) {
        return toAjax(iMaterialTypeService.updateById(baseTeam) ? 1 : 0);
    }

    /**
     * 删除物料类型
     */
    @Log(title = "物料单位" , businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除物料类型")
    @DeleteMapping("/{teamIds}" )
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(iMaterialTypeService.removeByIds(Arrays.asList(teamIds)) ? 1 : 0);
    }
}
